package org.myspringframework.springbootmybatisplus001.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.myspringframework.springbootmybatisplus001.bean.User;
import org.myspringframework.springbootmybatisplus001.mapper.UserMapper;
import org.myspringframework.springbootmybatisplus001.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author fzy
 * @version 1.0
 * 创建时间：       2024-09-24 19:52
 */

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    UserMapper userMapper;

    @Override
    @Transactional
    public boolean transfer(Long fromId, Long toId, int num) {
        User fromUser = userMapper.selectById(fromId);
        User toUser = userMapper.selectById(toId);
        if (fromUser == null || toUser == null || fromUser.getAge() < num) {
            return false;
        }
        fromUser.setAge(fromUser.getAge() - num);
        toUser.setAge(toUser.getAge() + num);
        int c1 = userMapper.updateById(fromUser);

        int a = 1 / 0;

        int c2 = userMapper.updateById(toUser);
        return c1 > 0 && c2 > 0;
    }
}
